<?php

namespace Aspic\Form\Decorator\Element;

use Aspic as A;
use Aspic\Form;
use Aspic\Form\Element\AbstractInput;

class Basic extends Form\Decorator\AbstractDecorator implements Form\Decorator\LabeledElementDecoratorInterface {

	protected function isRenderable(Form\Element\AbstractElement $el) {
		return ($el instanceof Form\Element\AbstractInput);
	}
	
	public function out($el) {
		$this->checkRenderable($el);
		
		$cb = new A\CodeBuilder;
		
		$cb->next($this->outLabelStart($el));
		$cb->nl($this->outLabelEnd($el));
		$cb->nl($this->outInput($el));
		
		return $cb->out();
	}

	public function outInput(Form\Element\AbstractElement $el) {
		$out = '<input type="'.$el->getInputType().'"'.$el->getAttrsAndNameStr().'/>';
			
		return $out;
	}

	public function outLabelEnd(Form\Element\AbstractElement $el) {
		$out = '</label>';
		
		return $out;
	}

	public function outLabelStart(Form\Element\AbstractElement $el) {
		$out = '<label for="'.$el->attr('id').'" class="'.$el->opt('labelClass').'">'.S::escapeXss($el->opt('labelText'));
		
		return $out;
	}
}